package first.endtoend

import org.springframework.dao.DataIntegrityViolationException

class PortfolioController
{

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
    def scaffold = Portfolio


    def index()
    {
        redirect(action: "list", controller: "family")
    }

    def list(Integer max)
    {
        redirect(action: "list", controller: "family")
    }

    def create()
    {
        [portfolioInstance: new Portfolio(params)]
    }

    def save()
    {
        def portfolioInstance = new Portfolio(params)
        Date date = new Date()
        Date maxValidity
        Family family = portfolioInstance.family

        def portfolio
        maxValidity = portfolioInstance.maxValidity
        if (!portfolioInstance.save(flush: true))
        {

            //test if family has a portfolio enabled then don't create another one!
            if (family.hasPortfolioEnabled())
            {
                println("error! family has already a portfolio enabled!!")
                //TODO add form to show error message
                render(view: "create", model: [portfolioInstance: portfolioInstance])
                return
            }
        } else if (!list && maxValidity > date)
        {
            println("there is not portfolios related to this family!!")


            flash.message = message(code: 'default.created.message', args: [message(code: 'portfolio.label', default: 'Portfolio'), portfolioInstance.id])
            redirect(action: "show", id: portfolioInstance.id)
            //see if there is already a portfolio that has a maxValidity> toDate
        }

    }

    def show(Long id)
    {
        def portfolioInstance = Portfolio.get(id)
        if (!portfolioInstance)
        {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'portfolio.label', default: 'Portfolio'), id])
            redirect(action: "list")
            return
        }

        [portfolioInstance: portfolioInstance]
    }

    def edit(Long id)
    {
        def portfolioInstance = Portfolio.get(id)
        if (!portfolioInstance)
        {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'portfolio.label', default: 'Portfolio'), id])
            redirect(action: "list")
            return
        }

        [portfolioInstance: portfolioInstance]
    }

    def update(Long id, Long version)
    {
        def portfolioInstance = Portfolio.get(id)
        if (!portfolioInstance)
        {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'portfolio.label', default: 'Portfolio'), id])
            redirect(action: "list")
            return
        }

        if (version != null)
        {
            if (portfolioInstance.version > version)
            {
                portfolioInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                        [message(code: 'portfolio.label', default: 'Portfolio')] as Object[],
                        "Another user has updated this Portfolio while you were editing")
                render(view: "edit", model: [portfolioInstance: portfolioInstance])
                return
            }
        }

        portfolioInstance.properties = params

        if (!portfolioInstance.save(flush: true))
        {
            render(view: "edit", model: [portfolioInstance: portfolioInstance])
            return
        }

        flash.message = message(code: 'default.updated.message', args: [message(code: 'portfolio.label', default: 'Portfolio'), portfolioInstance.id])
        redirect(action: "show", id: portfolioInstance.id)
    }

    def delete(Long id)
    {
        def portfolioInstance = Portfolio.get(id)
        if (!portfolioInstance)
        {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'portfolio.label', default: 'Portfolio'), id])
            redirect(action: "list")
            return
        }

        try
        {
            portfolioInstance.delete(flush: true)
            //TODO add Alert message "You don't have the right to delete a potfolio"!!
            flash.message = message(code: 'default.deleted.message', args: [message(code: 'portfolio.label', default: 'Portfolio'), id])
            redirect(action: "list")
        }
        catch (DataIntegrityViolationException e)
        {
            flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'portfolio.label', default: 'Portfolio'), id])
            redirect(action: "show", id: id)
        }
    }


}
